package schema

import (
	"time"

	"entgo.io/ent"
	"entgo.io/ent/dialect/entsql"
	"entgo.io/ent/schema"
	"entgo.io/ent/schema/edge"
	"entgo.io/ent/schema/field"
	"entgo.io/ent/schema/index"
)

// Department 部门实体
type Department struct {
	ent.Schema
}

// Fields 部门字段
func (Department) Fields() []ent.Field {
	return []ent.Field{
		field.Int64("id").
			Positive().
			Unique().
			Comment("部门ID"),
		field.String("name").
			NotEmpty().
			MaxLen(100).
			Comment("部门名称"),
		field.String("code").
			Optional().
			MaxLen(50).
			Comment("部门编码"),
		field.Text("description").
			Optional().
			Comment("部门描述"),
		field.Int64("parent_id").
			Optional().
			Nillable().
			StorageKey("parent_id").
			Comment("父部门ID"),
		field.Time("created_at").
			Default(time.Now).
			Immutable(),
		field.Time("updated_at").
			Default(time.Now).
			UpdateDefault(time.Now),
	}
}

// Edges 部门关联
func (Department) Edges() []ent.Edge {
	return []ent.Edge{
		edge.From("parent", Department.Type).
			Ref("children").
			Field("parent_id").
			Unique().
			Comment("父部门"),
		edge.To("children", Department.Type).
			Comment("子部门"),
		edge.To("activity_participations", ActivityParticipant.Type).
			Comment("该部门参与的活动记录"),
		edge.To("user_departments", UserDepartment.Type),
	}
}

// Indexes 索引
func (Department) Indexes() []ent.Index {
	return []ent.Index{
		index.Fields("name"),
		index.Fields("code"),
		index.Fields("parent_id"),
	}
}

// Annotations of the ConversationMember.
func (Department) Annotations() []schema.Annotation {
	return []schema.Annotation{
		entsql.Annotation{
			Table: "departments",
		},
	}
}
